package com.mdc.mansion.web.common.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;

import com.mdc.mansion.web.common.config.WebGlobal;
import com.mdc.mansion.web.common.context.WebAppContext;

/**
 * 一次完整请求的context的处理
 * @author chenmingdong
 *
 * 2016年1月29日
 *
 */
public class ReqWebContextInterceptor extends HandlerInterceptorAdapter{
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
//		WebAppContext.setRequestSeq(); //为本次请求生成请求序列
		int pageNo=1;
		int pageSize= WebGlobal.getInt("page_size");
		String spage=WebUtils.findParameterValue(request, "pageNo");
		String srow=WebUtils.findParameterValue(request, "pageSize");
		if(StringUtils.isNumeric(spage)){pageNo=Integer.valueOf(spage);}
		if(StringUtils.isNumeric(srow)){pageSize=Integer.valueOf(srow);}
		String orderBy=WebUtils.findParameterValue(request, "orderBy");
		WebAppContext.setContext(pageNo, pageSize,orderBy);
		return super.preHandle(request, response, handler);
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
//		WebAppContext.clearRequestSeq(); //清理本次请求的序列
		WebAppContext.clearContext();
		super.afterCompletion(request, response, handler, ex);
	}
	

}
